﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyClubMemberUpdForm
 * 类型：       控制层
 * 功能说明：   快乐管家-兴趣小组会员登记页面
 * 作者：       章 昕
 * 创建日期：   2007/09/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using Shpm.App.Common.Bean;

public partial class App_Input_HappyClubMemberUpdForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyClubMemberUpdForm));

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        txtMemberId.Attributes.Add("onblur", "showMemberName(this.id,'" + txtMemberName.ClientID + "');");
        //txtMemberName.Attributes.Add("onfocus", "document.getElementById('" + txtMemberId.ClientID + "').focus();");

        if (!Page.IsPostBack)
        {
            // 保存接收的参数
            ViewState["InpInterestName"] = Convert.ToString(Context.Items["InpInterestName"]);
            ViewState["InpInterestType"] = Convert.ToString(Context.Items["InpInterestType"]);
            ViewState["InpLeadName"] = Convert.ToString(Context.Items["InpLeadName"]);


            ViewState["HidInterestName"] = Convert.ToString(Context.Items["HidInterestName"]);
            ViewState["HidInterestType"] = Convert.ToString(Context.Items["HidInterestType"]);
            ViewState["HidLeadName"] = Convert.ToString(Context.Items["HidLeadName"]);



            if (Context.Items.Contains("HidCommand"))
            {
                ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);
                ViewState["HidPageIndex"] = Context.Items["HidPageIndex"];
            }


            // 俱乐部编号
            ViewState["INTEREST_ID"] = Convert.ToString(Context.Items["INTEREST_ID"]);
            ViewState["MEMBER_NO"] = "";
            ViewState["EDIT_MODE"] = "";
        }
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // get data
        bindSelectData(Convert.ToString(ViewState["INTEREST_ID"]));


    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
        { return; }

        // get memberid
        int index = Convert.ToInt32(e.CommandArgument);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];

        ViewState["MEMBER_NO"] = "";

        // club member info
        string memberNo = Convert.ToString(key.Value);
        if (!string.IsNullOrEmpty(memberNo)
            && !string.IsNullOrEmpty(Convert.ToString(ViewState["INTEREST_ID"])))
        {
            HappyClubMemberDao dao = new HappyClubMemberDao();
            HappyClubMemberBean bean = new HappyClubMemberBean();

            // 编辑 or 退会
            //lblTableTitle.Text = "入会信息编辑";
            bean.InterestId = Convert.ToInt32(Convert.ToString(ViewState["INTEREST_ID"]));
            bean.MemberNo = memberNo;
            bean = dao.FindClubMemberInfo(bean);
            if (bean != null)
            {
                txtMemberId.Text = bean.MemberNo;
                txtMemberName.Text = bean.MemberName;
                if (bean.BeginDate.HasValue)
                { txtAttendDate.Text = DateUtils.convertToString(bean.BeginDate.Value); }
                if (bean.EndDate.HasValue)
                { txtQuitDate.Text = DateUtils.convertToString(bean.EndDate.Value); }
                lstAttend.SelectedValue = "0";      // 正常在会
                if (bean.EndFlg.HasValue && bean.EndFlg.Value)
                { lstAttend.SelectedValue = "1"; }  // 已经退会

                txtQuitDate.Enabled = true;
                ViewState["MEMBER_NO"] = Convert.ToString(key.Value);
                ViewState["EDIT_MODE"] = "1";
            }
        }

    }

    // 保存按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            // do validate check
            // page 
            ComValidateChecker vd = new ComValidateChecker();
            vd.CheckItems.Add(new CheckRequired(txtMemberId.Text, new ComMessage("E0094", "会员编号")));
            vd.CheckItems.Add(new CheckRequired(lstAttend.SelectedValue, new ComMessage("E0094", "操作（入会/退会）")));
            vd.CheckItems.Add(new CheckRequired(txtAttendDate.Text, new ComMessage("E0094", "入会日期")));
            vd.CheckItems.Add(new CheckDate(txtAttendDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "入会日期")));
            if ("1".Equals(lstAttend.SelectedValue))
            {
                vd.CheckItems.Add(new CheckRequired(txtQuitDate.Text, new ComMessage("E0094", "退会日期")));
                vd.CheckItems.Add(new CheckDate(txtQuitDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "退会日期")));
            }

            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                ViewState["EDIT_MODE"] = "1";
                return;
            }

            HappyClubMemberDao dao = new HappyClubMemberDao();
            // person exist        
            ComMemberBean beanChk = new ComMemberBean();
            beanChk.MemberNo = txtMemberId.Text;
            if (dao.GetMember(beanChk) == null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "会员");
                ViewState["EDIT_MODE"] = "1";
                return;
            }

            // club exist
            if (string.IsNullOrEmpty(Convert.ToString(ViewState["INTEREST_ID"])))
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "俱乐部");
                ViewState["EDIT_MODE"] = "1";
                return;
            }
            HappyClubMemberBean bean = new HappyClubMemberBean();
            bean.InterestId = Convert.ToInt32(ViewState["INTEREST_ID"]);
            if (dao.FindClub(bean) == null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "俱乐部");
                ViewState["EDIT_MODE"] = "1";
                return;
            }

            // insert or update
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

            bean.InterestId = Convert.ToInt32(ViewState["INTEREST_ID"]);
            bean.MemberNo = txtMemberId.Text;
            bean = dao.FindClubMember(bean);

            if (bean != null)
            {
                // update                
                bean.BeginDate = DateUtils.convertToDate(txtAttendDate.Text);
                if ("1".Equals(lstAttend.SelectedValue))
                {
                    bean.EndDate = DateUtils.convertToDate(txtQuitDate.Text);
                    bean.EndFlg = true;
                }
                else
                {
                    bean.EndDate = null;
                    bean.EndFlg = false;
                }
                bean.UserId = loginId;
                dao.Update(bean);
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
            else
            {
                // insert
                bean = new HappyClubMemberBean();
                bean.InterestId = Convert.ToInt32(ViewState["INTEREST_ID"]);
                bean.MemberNo = txtMemberId.Text;
                bean.BeginDate = DateUtils.convertToDate(txtAttendDate.Text);
                bean.EndFlg = false;
                bean.UserId = loginId;
                dao.Insert(bean);
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }

            ViewState["MEMBER_NO"] = "";
            ViewState["EDIT_MODE"] = "";

        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }

    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        // 传递控件输入内容
        Context.Items["InpInterestName"] = ViewState["InpInterestName"];
        Context.Items["InpInterestType"] = ViewState["InpInterestType"];
        Context.Items["InpLeadName"] = ViewState["InpLeadName"];

        // 传递接收的参数
        Context.Items["HidInterestName"] = Convert.ToString(ViewState["HidInterestName"]);
        Context.Items["HidInterestType"] = Convert.ToString(ViewState["HidInterestType"]);
        Context.Items["HidLeadName"] = Convert.ToString(ViewState["HidLeadName"]);
        if (ViewState.IsItemDirty("HidCommand"))
        {
            Context.Items["HidCommand"] = Convert.ToString(ViewState["HidCommand"]);
            Context.Items["HidPageIndex"] = Convert.ToString(ViewState["HidPageIndex"]);
        }
        string url = "~/App/Input/HappyClubSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);    
    }

    // bind data to controls
    private void bindSelectData(string interestId)
    {

        if (string.IsNullOrEmpty(interestId))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "俱乐部信息");
            return;
        }
        
        HappyClubMemberDao dao = new HappyClubMemberDao();
        HappyClubMemberBean bean = new HappyClubMemberBean();

        // club info
        bean.InterestId = Convert.ToInt32(interestId);
        bean = dao.FindClub(bean);
        if (bean == null)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "俱乐部信息");
            return;
        }
        lblClubName.Text = bean.ClubName;
        lblClubType.Text = bean.ClubTypeName;
        lblClubLeader.Text = bean.LeaderName;
        lblClubAsst.Text = bean.AsstName;
        lblEmployee.Text = bean.EmpName;
        lblMemo.Text = bean.Memo;

        // club member list
        IList list = dao.ListClubMember(bean);
        tblMemList.Visible = false;
        if (list != null && list.Count > 0)
        {
            viewSrhRst.DataSource = list;
            viewSrhRst.DataBind();
            tblMemList.Visible = true;
        }

        // club member count
        bean = dao.GetClubMemberNum(bean);
        lblClubMemCnt.Text = "0";
        if (bean != null && !String.IsNullOrEmpty(bean.NumCnt))
        { lblClubMemCnt.Text = bean.NumCnt; }

        if (string.IsNullOrEmpty(Convert.ToString(ViewState["MEMBER_NO"]))
            && string.IsNullOrEmpty(Convert.ToString(ViewState["EDIT_MODE"])))
        {
            // 新入会
            //lblTableTitle.Text = "新入会登记";
            txtMemberId.Text = string.Empty;
            txtMemberName.Text = string.Empty;
            txtAttendDate.Text = string.Empty;//DateUtils.convertToString(DateTime.Now);
            txtQuitDate.Enabled = false;
            lstAttend.SelectedValue = "0";
            txtAttendDate.Text = "";
            txtQuitDate.Text = "";
        }
    }

    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[5].Text = "正常";
            if (((HappyClubMemberBean)e.Row.DataItem).EndFlg.HasValue
                && ((HappyClubMemberBean)e.Row.DataItem).EndFlg.Value)
            {
                e.Row.Cells[5].Text = "已退会";                
            }           
        }
    }
}
